﻿using UnityEngine;

/// <summary>
/// 冻结元素
/// </summary>
public class FreezeAura : IElementAura
{
    private const float MinGaugeLossSpeed = 0.4f; // 最小衰退速度

    private readonly IElementHolder _holder;
    private readonly IFreezable _freezable;
    private float _intervalTimer;
    private float _gauge;
    private float _gaugeLossSpeed = MinGaugeLossSpeed;

    public GameObject Source { get; set; }

    /// <summary>
    /// 元素量
    /// </summary>
    public float Gauge
    {
        get => _gauge;
        set
        {
            if (value <= 0f)
            {
                Debug.Log("冻结结束");
                _freezable?.Unfreeze();
                _gauge = 0f;
            }
            else
            {
                _gauge = value;
            }
        }
    }

    public ElementType ElementType => ElementType.Ice;

    public bool IsGaugeLoss { get; set; }

    public FreezeAura(IElementHolder holder)
    {
        _holder = holder;
        _freezable = _holder.Owner.GetComponent<IFreezable>();
    }

    public void Attach(float gauge, GameObject source)
    {
        Debug.Log("冻结开始");
        Gauge = gauge;
        Source = source;
        _freezable?.Freeze();
    }

    public void Update(float deltaTime)
    {
        if (Gauge > 0f)
        {
            // 增加衰退速度
            _gaugeLossSpeed += 0.1f * deltaTime;
            Gauge -= _gaugeLossSpeed * deltaTime;
        }
        else
        {
            if (_gaugeLossSpeed > MinGaugeLossSpeed)
            {
                // 减少衰退速度
                _gaugeLossSpeed -= 0.2f * deltaTime;
                if (_gaugeLossSpeed < MinGaugeLossSpeed)
                {
                    _gaugeLossSpeed = MinGaugeLossSpeed;
                }
            }
        }
    }
}